package com.genius.framework.multitenancy.rest;

import com.genius.framework.multitenancy.client.MultiTenancyClient;
import com.genius.framework.multitenancy.entity.TenantUser;
import com.genius.framework.multitenancy.service.TenantUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/api")
@RestController
public class TenantUserResource {

    @Autowired
    private TenantUserService tenantUserService;

    @Resource
    private MultiTenancyClient multiTenancyClient;

    @GetMapping("/tenant-users")
    public List<TenantUser> findAll() {
        return tenantUserService.findAllTenantUsers();
    }

    @GetMapping("/tenant-users/client")
    public List<TenantUser> findAllByClient() {
        return multiTenancyClient.findAll();
    }

    @PostMapping("/tenant-users")
    public TenantUser createTenantUser(@RequestBody TenantUser tenantUser) throws NoSuchFieldException {
        return tenantUserService.save(tenantUser);
    }

    @PostMapping("/tenant-users/random")
    public void createTenantUserRandom() throws NoSuchFieldException {
        tenantUserService.saveRandom();
    }

    @GetMapping("/tenant-users/tenant/{tenantId}")
    public List<TenantUser> findAllByTenantId(@PathVariable String tenantId) {
        return tenantUserService.findAllByTenantId(tenantId);
    }


}
